﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using ETL.Web.Filters;
using ETL.Web.Models;
using ETL.Entity;
using WebMatrix.WebData;

namespace EngineerTimeLog_mvc4.Controllers
{
    [InitializeSimpleMembership]
    public class UserController : Controller
    {

        #region Variables Declaration
        private ETLDataEntities db = new ETLDataEntities();

        #endregion

        #region Methods
        /// <summary>
        /// function create roles, create 2 default roles : User and Admin
        /// </summary>
        /// <returns></returns>
        public string CreateRoles()
        {
            if (!Roles.RoleExists("User"))
            {
                // create user role
                Roles.CreateRole("User");
            }
            if (!Roles.RoleExists("Admin"))
            {
                // create admin role
                Roles.CreateRole("Admin");
            }
            return "CreateRoles";
        }

        /// <summary>
        /// this function is to transfer admin account from old system to asp.net membership
        /// it takes data from table dbo.Project_Management then create a new user with Admin role
        /// </summary>
        /// <returns></returns>
        public string TransferAdminAccounts()
        {
            foreach (APP_USERS projectManagement in db.APP_USERS)
            {
                // Get the user name, default password is "remote747"
                string userName = projectManagement.DSE_ID;
                string password = "remote747";

                if (!WebSecurity.UserExists(userName))
                {
                    // Create new user
                    WebSecurity.CreateUserAndAccount(userName, password);
                }
                // Link user to role by create a new records in dbo.webpages_UsersInRoles
                Roles.AddUserToRole(userName, "Admin");

            }
            return "TransferAdminAccounts";
        }

        /// <summary>
        /// this function is to transfer all normal user account from old system to asp.net membership
        /// it takes data from table dbo.project_all_names then create a new user with User role
        /// </summary>
        /// <returns></returns>
        public string TransferUserAccounts()
        {
            foreach (APP_USERS projectAllName in db.APP_USERS)
            {
                // get username, default password is "remote747"
                string userName = projectAllName.DSE_ID;
                string password = "remote747";

                if (!WebSecurity.UserExists(userName))
                {
                    // Create new user
                    WebSecurity.CreateUserAndAccount(userName, password);
                }
                // Link user to role by create a new records in dbo.webpages_UsersInRoles
                Roles.AddUserToRole(userName, "User");

            }
            return "TransferUserAccounts";
        }
        #endregion

    }
}
